<!--
 * @Description: 导入超级管理员界面
 * @Version: 1.0
 * @Author: Yuanjy
 * @Date: 2020-03-15 20:45:02
 * @LastEditors: Yuanjy
 * @LastEditTime: 2020-05-08 09:45:22
 -->
<template>
  <div class="import-container">
    <el-button size="mini" type="primary" @click="to">导入用户</el-button>
    <el-dialog ref="importForm" :visible.sync="dialogShow" title="导入超级管理员">
      <el-tree
        ref="tree"
        :data="data"
        :props="defaultProps"
        :load="loadNode"
        lazy
        show-checkbox
        node-key="id"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancel">取消</el-button>
        <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  props: {
    sup_this: {
      type: Object,
      required: true
    },
    enterpriseId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      dialogShow: false,
      defaultProps: {
        label: 'name',
        children: 'children',
        isLeaf: 'is_leaf'
      },
      data: []
    }
  },
  methods: {
    to() {
      this.dialogShow = true
    },
    cancel() {
      this.dialogShow = false
    },
    doSubmit() {
      this.$api.enterprise.enterprise
        .saveSuperUser({
          data: { 
            enterprise_id: this.enterpriseId,
            user_ids: this.$refs.tree.getCheckedKeys()
          }
        })
        .then(res => {
          this.dialogShow = false
          this.$notify({ title: '提示', message: '超级管理员信息导入成功，请告知企业超级管理员', type: 'success', duration: 3000 })
        }).catch(err => {
          console.log(err)
          this.$notify({ title: '提示', message: '超级管理员信息导入失败', type: 'error', duration: 2500 })
        })
    },
    loadNode(node, resolve) {
      if (node.level === 0) {
        this.$api.enterprise.enterprise
          .getDeptList(this.enterpriseId)
          .then(res => {
            this.dialogShow = true
            resolve(res.data)
          }).catch(err => {
            this.dialogShow = false
            console.log(err)
          })
      }
      
      if (node.level === 1) {
        this.$api.enterprise.enterprise
          .getDeptUserSimpleList({
            params: { enterprise_id: this.enterpriseId, dept_id: node.data.id}
          })
          .then(res => {
            this.dialogShow = true
            resolve(res.data)
          }).catch(err => {
            this.dialogShow = false
            console.log(err)
          })
      }
      if (node.level > 1) return resolve([])

      // setTimeout(() => {
      //   const data = [{
      //     name: 'leaf',
      //     leaf: true
      //   }, {
      //     name: 'zone'
      //   }];

      //   resolve(data);
      // }, 500);
    }
  }
}
</script>

<style scoped>
  .import-container {
    display: inline-block;
  }
</style>
